package UserGift.dao;

import Hibernate_web.HibernateUtil;
import UserGift.model.Gift;
import org.hibernate.Session;


import java.util.Collection;
import java.util.List;

public class GiftDao implements IDao<Integer, Gift> {

    @Override
    public Gift getById(Integer id) {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        Gift gift = (Gift) session.load(Gift.class, id);
        session.getTransaction().commit();
        return gift;

    }

    @Override
    public Collection<Gift> getAll() {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        List<Gift> gifts = session.createCriteria(Gift.class).list();
        session.getTransaction().commit();
        return gifts;
    }

    @Override
    public void save(Gift gift) {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        session.save(gift);
        session.getTransaction().commit();
    }

    @Override
    public void update(Gift gift) {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        session.update(gift);
        session.getTransaction().commit();
    }
}
